package com.itfreer.gis.data.server.geoserver;

import org.geotools.geometry.jts.JTSFactoryFinder;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.LinearRing;
import org.locationtech.jts.geom.MultiLineString;
import org.locationtech.jts.geom.MultiPoint;
import org.locationtech.jts.geom.MultiPolygon;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;

/**
 * GeoServer处理类 
 */
public class GeoServeUtils {

	private final static GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);

	// 坐标串反转
	public static Coordinate[] inverse(Geometry geometry) {
		Coordinate[] coordinates = geometry.getCoordinates();

		Coordinate[] inverseCoordinates = new Coordinate[coordinates.length];
		for (int i = 0; i < coordinates.length; i++) {
			Coordinate coordinate = new Coordinate(coordinates[i].getY(), coordinates[i].getX());
			inverseCoordinates[i] = coordinate;
		}
		return inverseCoordinates;

	}

	// 构建点
	public static Point createPoint(Coordinate[] coordinates) {
		return geometryFactory.createPoint(coordinates[0]);
	}

	// 构建点
	public static Point createPoint(double x, double y) {
		Coordinate coord = new Coordinate(x, y);
		Point point = geometryFactory.createPoint(coord);
		return point;
	}

	// 构建点
	public static Point createPointByWKT(String geomotryWkt) throws ParseException {
		WKTReader reader = new WKTReader(geometryFactory);
		Point point = (Point) reader.read(geomotryWkt);// "POINT (109.013388 32.715519)"
		return point;
	}

	// 构建多个点
	public static MultiPoint createMulPointByWKT(String geomotryWkt) throws ParseException {
		WKTReader reader = new WKTReader(geometryFactory);
		MultiPoint mpoint = (MultiPoint) reader.read(geomotryWkt);// "MULTIPOINT(109.013388 32.715519,119.32488
																	// 31.435678)"
		return mpoint;
	}

	// 构建多个点
	@SuppressWarnings("deprecation")
	public static MultiPoint create(Coordinate[] coordinates) {
		return geometryFactory.createMultiPoint(coordinates);
	}

	// 构建线
	public static LineString createLine(Coordinate[] coordinates) {
		return geometryFactory.createLineString(coordinates);
	}

	// 构建线
	public static LineString createLineByWKT(String geomotryWkt) throws ParseException {
		WKTReader reader = new WKTReader(geometryFactory);
		LineString line = (LineString) reader.read(geomotryWkt);// "LINESTRING(0 0, 2 0)"
		return line;
	}

	// 构建多条线
	public static MultiLineString createMLine(LineString[] lineStrings) {
		MultiLineString ms = geometryFactory.createMultiLineString(lineStrings);
		return ms;
	}

	// 构建多条线
	public static MultiLineString createMLineByWKT(String geomotryWkt) throws ParseException {
		WKTReader reader = new WKTReader(geometryFactory);
		MultiLineString line = (MultiLineString) reader.read(geomotryWkt);// "MULTILINESTRING((0 0, 2 0),(1 1,2 2))"
		return line;
	}

	// 构建多边形
	public static Polygon createPolygon(Coordinate[] coordinates) {
		return geometryFactory.createPolygon(coordinates);
	}

	// 构建多边形
	public static Polygon createPolygonByWKT(String geomtry) throws ParseException {
		WKTReader reader = new WKTReader(geometryFactory);
		Polygon polygon = (Polygon) reader.read(geomtry);// POLYGON((40 10, 30 0, 40 10, 30 20, 40 10))
		return polygon;
	}

	// 构建多多边形
	public static MultiPolygon createMulPolygon(Polygon[] polygons) throws ParseException {
		return geometryFactory.createMultiPolygon(polygons);
	}

	// 构建多多边形
	public static MultiPolygon createMulPolygonByWKT(String geomtry) throws ParseException {
		WKTReader reader = new WKTReader(geometryFactory);
		MultiPolygon mpolygon = (MultiPolygon) reader.read(geomtry);// "MULTIPOLYGON(((40 10, 30 0, 40 10, 30 20, 40
																	// 10),(30 10, 30 0, 40 10, 30 20, 30 10)))"
		return mpolygon;
	}

	/*
	 * //构建geo集合 public static GeometryCollection createGeoCollect() throws
	 * ParseException { LineString line = createLine(); Polygon poly =
	 * createPolygonByWKT(); Geometry g1 = geometryFactory.createGeometry(line);
	 * Geometry g2 = geometryFactory.createGeometry(poly); Geometry[] garray = new
	 * Geometry[] { g1, g2 }; GeometryCollection gc =
	 * geometryFactory.createGeometryCollection(garray); return gc; }
	 */

	// 构建圆
	public static Polygon createCircle(double x, double y, final double RADIUS) {
		final int SIDES = 32;// 圆上面的点个数
		Coordinate coords[] = new Coordinate[SIDES + 1];
		for (int i = 0; i < SIDES; i++) {
			double angle = ((double) i / (double) SIDES) * Math.PI * 2.0;
			double dx = Math.cos(angle) * RADIUS;
			double dy = Math.sin(angle) * RADIUS;
			coords[i] = new Coordinate((double) x + dx, (double) y + dy);
		}
		coords[SIDES] = coords[0];
		LinearRing ring = geometryFactory.createLinearRing(coords);
		Polygon polygon = geometryFactory.createPolygon(ring, null);
		return polygon;
	}

	// 构建圆
	public static Polygon createCircle(Coordinate[] coordinates) {
		LinearRing ring = geometryFactory.createLinearRing(coordinates);
		Polygon polygon = geometryFactory.createPolygon(ring, null);
		return polygon;
	}

}
